/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CapShipUI.java
 *
 * Created on 19-Nov-2010, 21:59:45
 */
package camp.anchors;

import camp.anchors.Ship.Instance;
import camp.jme.Preview;
import campskeleton.CampSkeleton;
import campskeleton.Plan;
import campskeleton.PlanSkeleton;
import campskeleton.Profile;
import java.io.File;
import javax.swing.JOptionPane;
import straightskeleton.ui.Bar;
import straightskeleton.ui.Marker;
import straightskeleton.ui.NaiveMould;

/**
 * Debug routine to output many .obj files that we can then render nicely!
 * @author twak
 */
public class CapShipUI extends javax.swing.JPanel
{
    CapShip cs;
    Plan plan;

    /** Creates new form CapShipUI */
    public CapShipUI()
    {
        initComponents();
    }

    public CapShipUI( CapShip cs, Plan plan )
    {
        this.cs = cs;
        this.plan = plan;
        initComponents();

        atZeroHeightCheck.setSelected(cs.atZeroHeight);
        capCheck.setSelected(cs.addCap);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        fileField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        outputButton = new javax.swing.JButton();
        skelCheck = new javax.swing.JCheckBox();
        capCheck = new javax.swing.JCheckBox();
        meshCheck = new javax.swing.JCheckBox();
        stepSpinner = new javax.swing.JSpinner();
        atZeroHeightCheck = new javax.swing.JCheckBox();

        jLabel1.setText("*debug*");

        fileField.setText("C:\\Users\\twak\\complexmeshes\\");
            fileField.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    fileFieldActionPerformed(evt);
                }
            });

            jLabel3.setText("output dir:");

            jLabel4.setText("steps:");

            outputButton.setText("output obj's");
            outputButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    outputButtonActionPerformed(evt);
                }
            });

            skelCheck.setSelected(true);
            skelCheck.setText("sides");

            capCheck.setSelected(true);
            capCheck.setText("cap");
            capCheck.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    capCheckActionPerformed(evt);
                }
            });

            meshCheck.setSelected(true);
            meshCheck.setText("meshes");

            stepSpinner.setValue(350);

            atZeroHeightCheck.setText("cap at zero height");
            atZeroHeightCheck.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    atZeroHeightCheckActionPerformed(evt);
                }
            });

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
            this.setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel1)
                        .addComponent(skelCheck)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(capCheck)
                            .addGap(18, 18, 18)
                            .addComponent(atZeroHeightCheck))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel3)
                                .addComponent(jLabel4))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(stepSpinner, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)
                                .addComponent(fileField, javax.swing.GroupLayout.DEFAULT_SIZE, 195, Short.MAX_VALUE)))
                        .addComponent(meshCheck))
                    .addContainerGap())
                .addComponent(outputButton, javax.swing.GroupLayout.DEFAULT_SIZE, 260, Short.MAX_VALUE)
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(jLabel1)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(fileField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel3))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel4)
                        .addComponent(stepSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(skelCheck)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(capCheck)
                        .addComponent(atZeroHeightCheck))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(meshCheck)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE)
                    .addComponent(outputButton))
            );
        }// </editor-fold>//GEN-END:initComponents

    private void fileFieldActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_fileFieldActionPerformed
    {//GEN-HEADEREND:event_fileFieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_fileFieldActionPerformed

    private void outputButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_outputButtonActionPerformed
    {//GEN-HEADEREND:event_outputButtonActionPerformed
        Marker heightEdit = null;
        
        // find a CapShip instance
        for ( Profile p : plan.findProfiles() )
            for ( Bar b : p.points.eIterator() )
                for ( Marker m : b.mould.getAnchorsForEditing( b, b.start, b.end ) )
                    for ( Instance ins : cs.getInstances() )
                        if ( ins.anchors[0].matches( null, m.generator ) )
                        {
                            heightEdit = m;
                        }

        if ( heightEdit == null )
            return;

        double tHeight = -heightEdit.y;
        int steps = ((Integer) stepSpinner.getValue());

        cs.addCap = capCheck.isSelected();

        for ( int i = 0; i < steps; i++)
            try
            {

                double height = tHeight * i/ steps;
                heightEdit.y = -height;
                ((NaiveMould.PMarker)heightEdit.generator).setOffLine( heightEdit );

                PlanSkeleton s = new PlanSkeleton( plan );
                s.skeleton();

                if ( s.output.faces != null )
                {
                    Preview preview = CampSkeleton.instance.preview;

                    preview.clear = true;
                    preview.threadKey = s;
                    
                    preview.display( s.output, true, true, false );

                    System.out.println("1) now dumping "+i);
//                    preview.takeScreenShot(String.format ( fileField.getText() + "%04d.png", i) );

//                    if ( meshCheck.isSelected() )
//                        s.addMeshesTo( preview );

                    do
                        Thread.sleep( 100 );
                    while (preview.isPendingUpdate());
                    
                    preview.dump( new File( String.format( fileField.getText() + "m1/%04d.obj", i ) ) );
                    
                    preview.clear = true;
                    preview.threadKey = s;
                    
                    preview.display( s.output, false, false, true );

                    System.out.println("2) now dumping "+i);
//                    preview.takeScreenShot(String.format ( fileField.getText() + "%04d.png", i) );
                    do
                        Thread.sleep( 100 );
                    while (preview.isPendingUpdate());

//                    if ( meshCheck.isSelected() )
//                        s.addMeshesTo( preview );
                    
                    preview.dump( new File( String.format( fileField.getText() + "m2/%04d.obj", i ) ) );


                }
            }
            catch ( Exception ex )
            {
                JOptionPane.showMessageDialog( this, "error saving file :(" );
                ex.printStackTrace();
                return;
            }

//        heightEdit.y = -tHeight;
        ((NaiveMould.PMarker)heightEdit.generator).set( heightEdit );

        // flush changes from preview
        CampSkeleton.instance.somethingChanged();
    }//GEN-LAST:event_outputButtonActionPerformed

    private void atZeroHeightCheckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_atZeroHeightCheckActionPerformed
        cs.atZeroHeight = atZeroHeightCheck.isSelected();
    }//GEN-LAST:event_atZeroHeightCheckActionPerformed

    private void capCheckActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_capCheckActionPerformed
        cs.addCap = capCheck.isSelected();
    }//GEN-LAST:event_capCheckActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox atZeroHeightCheck;
    private javax.swing.JCheckBox capCheck;
    private javax.swing.JTextField fileField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JCheckBox meshCheck;
    private javax.swing.JButton outputButton;
    private javax.swing.JCheckBox skelCheck;
    private javax.swing.JSpinner stepSpinner;
    // End of variables declaration//GEN-END:variables
}
